package com.gh.duixiang01;

/**
 * 学号：number,年级grade（int）.成绩score（int）
 * 创建20个学成对象，学号从1到20，年级和成绩由随机数 确定
 * 1.打印3年级学生成绩
 * 2.使用冒泡排序遍历学生成绩并打印
 *
 *
 */

public class StudentTest {

    public static void main(String[] args) {
        Student[] stus = new Student[20];

        StudentTest test = new StudentTest();
        //初始化数组》》打印
        test.dateInfo(stus);
        test.print(stus);

        System.out.println("___________________________");
        //打印年级为3学生
        int findGrade = 3;
        test.find(stus,findGrade);
        System.out.println("----------------------------");
        //按成绩冒泡排序

        test.sort(stus);
        test.print(stus);
    }

    public Student[] dateInfo(Student[] stus){
        for (int i = 0; i < stus.length; i++) {
            //创建对象
            stus[i] = new Student();
            //给对象赋值
            stus[i].number = i+1;
            //Math.random*(max*-min)+min(生成大于等于min小于max的double型随机数；)左闭右开
            //年级random().6+1 [1,6]
            stus[i].grade = (int)(Math.random()*(6-1+1)+1);
            //成绩random()*101 [0,100]
            stus[i].score = (int)(Math.random()*(100-0)+1);
        }
        return stus;
    }

    /**
     * 打印
     * @param student
     */
    public void print(Student[] student){
        Student[] stus =student;
        for (int i = 0; i < stus.length; i++) {
//            System.out.println(stus[i].number+","+stus[i].grade+","+stus[i].score);

            System.out.println(stus[i].info());

        }
    }

    /**
     * 查找年级为3的学生
     * @param students
     */
    public void find(Student[] students,int findGrade){
        Student[] stus = students;
        for (int i = 0; i < stus.length; i++) {
            if(stus[i].grade ==findGrade){
                System.out.println(stus[i].info());
            }
        }
    }

    /**
     * 冒泡排序，成绩排
     * @param stus
     */
    public void sort(Student[] stus){
        for (int i = 0; i < stus.length; i++) {
            for (int j = 0; j < stus.length-1; j++) {
                if(stus[j].score > stus[j+1].score){
                    Student temp = stus[j];
                    stus[j] = stus[j+1];
                    stus[j+1] = temp;
                }
            }
        }

    }

}


class Student {
    int number;
    int grade;
    int score;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }


    public String info(){
        return "学号:"+ this.number+"年级:"+this.grade+"成绩："+this.score;
    }
}